import '../public/index'
import url from '../data/url'
import request from '../data/request'
import { tableConfig, header } from '../data/config'
import { getUrlParam } from '../data/util'

const base                   = url.base,
      getPOSKeyboardBind     = request.getPOSKeyboardBind,
      deletePOSKeyboardBind  = request.deletePOSKeyboardBind;

let table,
    keyboardId = getUrlParam('keyboardId');

// 初始化表格
table = $('#table').bootstrapTable($.extend({}, tableConfig, {
  url: base + getPOSKeyboardBind,
  queryParams: function (params) {
    return {
      currentPage : params.pageNumber,
      pageSize    : params.pageSize,
      keyboardId  : keyboardId
  }},
  columns: [
    {
      field: 'keySnVersion',
      title: '商户SN版本号 ',
      valign: 'middle'
    }, {
      field: 'keySN',
      title: '键盘设备码',
      valign: 'middle'
    }, {
      field: 'mchName',
      title: 'POS商户名称',
      valign: 'middle'
    }, {
      field: 'terminalNo',
      title: '键盘对应终端号',
      valign: 'middle'
    }, {
      field: 'rawAddTime',
      title: '添加时间',
      valign: 'middle',
      formatter: (index, row) => {
        return moment(index).format('YYYY-MM-DD HH:mm:ss')
      }
    }, {
      field: 'action',
      valign: 'middle',
      align: 'center',
      title: '操作',
      formatter: function(index, row) {
        return `<div class="btn-group">
                      <button type="button" data-id="${ row.idStr }" class="btn btn-default btn-unbind">解绑</button>                  
                </div>`
      }
    }]
}))


// 解绑
$(document).on('click', '#table .btn-unbind', (e) => {
  e.preventDefault()

  console.log('解绑')

  let id     = $(e.target).attr('data-id'),
      data   = {id: id};

  bootbox.confirm({
    title: '提示',
    message: `<div>
                <h3 style="margin:.75em 0">是否解绑？</h3>
              </div>`,
    buttons: {
      cancel: {
        label: '取消'
      },
      confirm: {
        label: '解绑'
      }
    },
    callback: (result) => {

      if(!result){
        return
      }

      let loading_modal = layer.msg('提交中...', {
        icon: 16,
        time: 0
      })

      $.ajax({
        headers: header,
        url : base + deletePOSKeyboardBind,
        type: 'POST',
        data: data
      })
        .done(function (res) {
          console.log(res)

          if( res.code == 0 ){

            layer.msg('解绑成功', {
              icon: 6
            })

            table.bootstrapTable('refresh')

          }else {
            layer.msg('解绑失败', {
              icon: 5
            })
          }

        })
        .fail(function (res) {
          console.log(res)

          layer.msg('解绑失败', {
            icon: 5
          })

        })
        .always(function(res) {

          console.log("complete")

          layer.close(loading_modal)
        })
    }
  })
})

$('#bind').on('click', (e) => {
  e.preventDefault;

  window.location.href = './addRelation.html?keyboardId=' + getUrlParam('keyboardId');
})